﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PTTK_HTTT_06.BUS;
using System.Data;

namespace PTTK_HTTT_06.DAO
{
    class NHAN_VIEN_DB
    {
        #region public static int KiemTraTaiKhoan(string TenTaiKhoan, string MatKhau)
        public static int KiemTraTaiKhoan(string TenTaiKhoan, string MatKhau)
        {
            DataProvider dataProvider = new DataProvider();

            //kiem tra admin trước
            string sqlStr = "select * from THAM_SO where TaiKhoanAdmin = '" + TenTaiKhoan + "' and MatKhauAdmin = '" + MatKhau + "'";
            dataProvider.Connect();
            DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
            

            if (dt != null && dt.Rows.Count > 0)
                return 0;
            else
            {
                //kiem tra người dùng
                sqlStr = "select MaNhanVien from NHAN_VIEN Where TenDangNhap = '" + TenTaiKhoan + "' and MatKhau = '" + MatKhau + "'";
                if (!dataProvider.IsConnect())
                    dataProvider.Connect();
                dt = dataProvider.ExcuteQueryDataTable(sqlStr);
                
                if (dt != null && dt.Rows.Count > 0) {
                    dataProvider.Disconnect();
                    return int.Parse(dt.Rows[0][0].ToString());
                }

            }

            //ko tim dc ng dung nao ca
            dataProvider.Disconnect();
            return -1;
        }
        #endregion

        #region public static bool KiemTraTinhTrangKhoa(int MaNhanVien)
        public static bool KiemTraTinhTrangKhoa(int MaNhanVien)
        {
            DataProvider dataProvider = new DataProvider();
            string sqlStr = "select MaNhanVien from NHAN_VIEN where MaNhanVien = '" + MaNhanVien + "' and TinhTrangKhoa = 'False'";

            if (!dataProvider.IsConnect())
                dataProvider.Connect();
            DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
            dataProvider.Disconnect();

            if (dt != null && dt.Rows.Count > 0)
                return false;
            return true;
        }
        #endregion

        #region public static NHAN_VIEN LayThongTin(int MaNhanVien)
        public static NHAN_VIEN LayThongTin(int MaNhanVien){
            NHAN_VIEN nv = new NHAN_VIEN();

            DataProvider dataProvider = new DataProvider();
            string sqlStr = "select * from NHAN_VIEN where MaNhanVien = '" + MaNhanVien + "'";
            if (!dataProvider.IsConnect())
                dataProvider.Connect();
            DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
            dataProvider.Disconnect();

            if (dt != null && dt.Rows.Count <= 0)
                return null;

            nv.MaNhanVien = int.Parse(dt.Rows[0]["MaNhanVien"].ToString());
            nv.MaBoPhan = int.Parse(dt.Rows[0]["MaBoPhan"].ToString());
            nv.MatKhau = dt.Rows[0]["MatKhau"].ToString();
            nv.TenDangNhap = dt.Rows[0]["TenDangNhap"].ToString();
            nv.TenNhanVien = dt.Rows[0]["TenNhanVien"].ToString();
            nv.TinhTrangKhoa = ((dt.Rows[0]["TenNhanVien"].ToString() == "True")?true:false);
            nv.CMND = dt.Rows[0]["CMND"].ToString();
            nv.DiaChi = dt.Rows[0]["DiaChi"].ToString();

            return nv;
        }
        #endregion
    }
}
